# base image
FROM node:14.17.0-alpine as build-stage

# set working directory
WORKDIR /forms-flow-web/app

ARG NODE_ENV
ARG GENERATE_SOURCEMAP
ARG REACT_APP_CLIENT_ROLE
ARG REACT_APP_STAFF_REVIEWER_ROLE
ARG REACT_APP_STAFF_DESIGNER_ROLE
ARG REACT_APP_CLIENT_ID
ARG REACT_APP_STAFF_REVIEWER_ID
ARG REACT_APP_STAFF_DESIGNER_ID
ARG REACT_APP_USER_RESOURCE_FORM_ID
ARG REACT_APP_API_SERVER_URL
ARG REACT_APP_API_PROJECT_URL
ARG REACT_APP_KEYCLOAK_CLIENT
ARG REACT_APP_ANONYMOUS_ID
ARG REACT_APP_INSIGHT_API_BASE
ARG REACT_APP_INSIGHTS_API_KEY
ARG REACT_APP_WEB_BASE_URL
ARG REACT_APP_CAMUNDA_API_URI
ARG REACT_APP_WEBSOCKET_ENCRYPT_KEY
ARG REACT_APP_KEYCLOAK_URL_REALM
ARG REACT_APP_KEYCLOAK_URL
ARG REACT_APP_APPLICATION_NAME
ARG REACT_APP_USER_ACCESS_PERMISSIONS
ARG REACT_APP_WEB_BASE_CUSTOM_URL

ENV NODE_ENV ${NODE_ENV}
ENV GENERATE_SOURCEMAP ${GENERATE_SOURCEMAP}
ENV REACT_APP_CLIENT_ROLE ${REACT_APP_CLIENT_ROLE}
ENV REACT_APP_STAFF_REVIEWER_ROLE ${REACT_APP_STAFF_REVIEWER_ROLE}
ENV REACT_APP_STAFF_DESIGNER_ROLE ${REACT_APP_STAFF_DESIGNER_ROLE}
ENV REACT_APP_CLIENT_ID ${REACT_APP_CLIENT_ID}
ENV REACT_APP_STAFF_REVIEWER_ID ${REACT_APP_STAFF_REVIEWER_ID}
ENV REACT_APP_STAFF_DESIGNER_ID ${REACT_APP_STAFF_DESIGNER_ID}
ENV REACT_APP_USER_RESOURCE_FORM_ID ${REACT_APP_USER_RESOURCE_FORM_ID}
ENV REACT_APP_API_SERVER_URL ${REACT_APP_API_SERVER_URL}
ENV REACT_APP_API_PROJECT_URL ${REACT_APP_API_PROJECT_URL}
ENV REACT_APP_KEYCLOAK_CLIENT ${REACT_APP_KEYCLOAK_CLIENT}
ENV REACT_APP_ANONYMOUS_ID ${REACT_APP_ANONYMOUS_ID}
ENV REACT_APP_INSIGHT_API_BASE ${REACT_APP_INSIGHT_API_BASE}
ENV REACT_APP_INSIGHTS_API_KEY ${REACT_APP_INSIGHTS_API_KEY}
ENV REACT_APP_WEB_BASE_URL ${REACT_APP_WEB_BASE_URL}
ENV REACT_APP_CAMUNDA_API_URI ${REACT_APP_CAMUNDA_API_URI}
ENV REACT_APP_WEBSOCKET_ENCRYPT_KEY ${REACT_APP_WEBSOCKET_ENCRYPT_KEY}
ENV REACT_APP_KEYCLOAK_URL_REALM ${REACT_APP_KEYCLOAK_URL_REALM}
ENV REACT_APP_KEYCLOAK_URL ${REACT_APP_KEYCLOAK_URL}
ENV REACT_APP_APPLICATION_NAME ${REACT_APP_APPLICATION_NAME}
ENV REACT_APP_USER_ACCESS_PERMISSIONS ${REACT_APP_USER_ACCESS_PERMISSIONS}
ENV REACT_APP_WEB_BASE_CUSTOM_URL ${REACT_APP_WEB_BASE_CUSTOM_URL}

# add `/app/node_modules/.bin` to $PATH
ENV PATH /forms-flow-web/app/node_modules/.bin:$PATH

RUN apk update && apk upgrade && \
    apk add --no-cache bash git openssh

# install and cache app dependencies

COPY package-lock.json /forms-flow-web/app/package-lock.json
COPY package.json /forms-flow-web/app/package.json
COPY forms-flow-util/package.json /forms-flow-web/app/forms-flow-util/package.json
COPY forms-flow-util/package-lock.json /forms-flow-web/app/forms-flow-util/package-lock.json

RUN npm install --unsafe-perm
#RUN npm install react-scripts@3.0.1 -g --silent
COPY . /forms-flow-web/app/
RUN npm run build

FROM nginx:1.17 as production-stage
RUN mkdir /app
COPY --from=build-stage /forms-flow-web/app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8080:8080
CMD ["nginx", "-g", "daemon off;"]
